package com.xqq.lottery.protocal;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.xmlpull.v1.XmlSerializer;

import android.util.Log;
import android.util.Xml;


/**
 * body
 * @author qqxia
 *
 */
public class Body {
	
	private List<Element> elements =new ArrayList<Element>();
	
	/*******处理服务器回复*******/
	private String bodyInsideDESInfo;  //服务器回复的body中DES加密
	
	public String getBodyInsideDESInfo() {
		return bodyInsideDESInfo;
	}

	public void setBodyInsideDESInfo(String bodyInsideDESInfo) {
		this.bodyInsideDESInfo = bodyInsideDESInfo;
	}

	/*******处理服务器回复*******/
	
	public void serializerBody(XmlSerializer serializer){
		try{
			serializer.startTag(null, "body");
			serializer.text(getBodyInsideDESInfo(getDesContent()));
			serializer.endTag(null, "body");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String getBodyContent(){
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter strWriter =new StringWriter();
		try {
			serializer.setOutput(strWriter);
			serializer.startTag(null, "body");
			serializer.startTag(null, "elements");
			for(Element elt : elements){
				elt.serializerElement(serializer);
			}
			serializer.endTag(null, "elements");
			serializer.endTag(null, "body");
			serializer.flush();
			return strWriter.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return "";
	}

	public List<Element> getElements() {
		return elements;
	}
	
	public String getDesContent(){
		String content = getBodyContent();
		Log.i("FlowLayout", "content="+content);
		String bodyContent = StringUtils.substringBetween(content, "<body>", "</body>");
		Log.i("FlowLayout", "bodyContent="+bodyContent);
		return bodyContent==null?"":bodyContent;
	}
	
	/**
	 * 加密
	 * @return
	 */
	public native String getBodyInsideDESInfo(String content);
	
}
